package taskhandler

import (
	"cvevulner/obs"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/logs"
	"io/ioutil"
	"os"
)

func ObsInit() *obs.ObsClient {
	var ak = beego.AppConfig.String("obs::access_key_id")
	var sk = beego.AppConfig.String("obs::secret_access_key")
	var endpoint = beego.AppConfig.String("obs::endpoint")
	var obsClient, _ = obs.New(ak, sk, endpoint)
	return obsClient
}

func ObsDeleteObject(obsDir string) {
	input := &obs.DeleteObjectInput{}
	input.Bucket = beego.AppConfig.String("obs::bucket")
	input.Key = obsDir
	obsClient := ObsInit()
	output, err := obsClient.DeleteObject(input)
	if err == nil {
		logs.Info("ObsDeleteObject, obsDir: ",obsDir, ",RequestId:%s\n", output.RequestId)
	} else if obsError, ok := err.(obs.ObsError); ok {
		logs.Error("ObsDeleteObject, obsDir: ",obsDir, ",Code:%s\n", obsError.Code)
		logs.Error("ObsDeleteObject, obsDir: ",obsDir, ",Message:%s\n", obsError.Message)
	}
}

func ObsCreateDir(obsDir string) (error, string) {
	ObsDeleteObject(obsDir)
	input := &obs.PutObjectInput{}
	input.Bucket = beego.AppConfig.String("obs::bucket")
	input.Key = obsDir
	obsClient := ObsInit()
	output, err := obsClient.PutObject(input)
	if err == nil {
		logs.Info("ObsCreateDir, obsDir: ",obsDir, ",obs, RequestId:%s\n", output.RequestId)
	} else if obsError, ok := err.(obs.ObsError); ok {
		logs.Error("ObsCreateDir, obsDir: ",obsDir, ",obs, Code:%s\n", obsError.Code)
		logs.Error("ObsCreateDir, obsDir: ",obsDir, ",obs, Message:%s\n", obsError.Message)
	}
	return err, obsDir
}

func ObsUploadFile(objectName, localfile string) error {
	input := &obs.PutFileInput{}
	input.Bucket = beego.AppConfig.String("obs::bucket")
	input.Key = objectName
	input.SourceFile = localfile
	obsClient := ObsInit()
	output, err := obsClient.PutFile(input)
	if err == nil {
		logs.Info("objectName: ",objectName, ",RequestId:%s\n", output.RequestId)
		logs.Info("ETag:%s\n", output.ETag)
	} else if obsError, ok := err.(obs.ObsError); ok {
		logs.Error("objectName: ",objectName, ",Code:%s\n", obsError.Code)
		logs.Error("objectName: ",objectName, ",Message:%s\n", obsError.Message)
	}
	return err
}

func ObsDownloadFile(objectName, localfile string) error {
	input := &obs.GetObjectInput{}
	input.Bucket = beego.AppConfig.String("obs::bucket")
	downDir := beego.AppConfig.String("obs::download_parent_directory")
	input.Key = downDir + objectName
	obsClient := ObsInit()
	output, err := obsClient.GetObject(input)
	if err != nil {
		logs.Error("ObsDownloadFile, obsClient.GetObject, err: ", err)
		return err
	}
	defer func() {
		errMsg := output.Body.Close()
		if errMsg != nil {
			logs.Error("ObsDownloadFile, output.Body.Close, errMsg: ", errMsg)
		}
	}()
	logs.Info("Object content:")
	body, err := ioutil.ReadAll(output.Body)
	if err != nil {
		logs.Error("ObsDownloadFile, ReadAll, err: ", err)
		return err
	}
	logs.Info(string(body))
	f, fErr := os.Create(localfile)
	if fErr != nil {
		logs.Error("ObsDownloadFile, fErr: ", fErr)
		return fErr
	}
	defer f.Close()
	_, err = f.Write(body)
	return nil
}
